package day7.LC707;

import java.security.SignatureException;

public class Main {
    public static void main(String[] args) {

    }
}

//采用虚拟头节点
//未知错误、无法调试、稍后再看
class MyLinkedList {
    int size;
    SingleLinkNode head;
    public MyLinkedList() {
        size=0;
        head=new SingleLinkNode(0);
    }

    public int get(int index) {
        if (index < 0 || index>size-1) {
            return -1;
        }
        SingleLinkNode currentNode = head;
        for (int i = 0; i <= index; i++) {
            currentNode=currentNode.next;
        }
        return currentNode.value;
    }

    public void addAtHead(int val) {
        SingleLinkNode newNode = new SingleLinkNode(val);
        newNode.next=head.next;
        head.next=newNode;
        size++;
    }

    public void addAtTail(int val) {
        SingleLinkNode currentNode=head;
        while (currentNode.next != null) {
            currentNode=currentNode.next;
        }
        SingleLinkNode newNode = new SingleLinkNode(val);
        currentNode.next=newNode;
        size++;
    }

    public void addAtIndex(int index, int val) {
        if (index < 0 || index >= size) {
            return;
        }

        //插入
        SingleLinkNode currentNode=head;
        for (int i = 0; i < index; i++) {
            currentNode=currentNode.next;
        }
        SingleLinkNode newNode = new SingleLinkNode(val);
        newNode.next=currentNode.next;
        currentNode.next=newNode;
        size++;
    }

    public void deleteAtIndex(int index) {
        if (index < 0 || index >= size) {
            return;
        }
        //删除
        SingleLinkNode currentNode=head;
        for (int i = 0; i < index; i++) {
            currentNode=currentNode.next;
        }
        currentNode.next=currentNode.next.next;
        size--;
    }
}

//单链表
class SingleLinkNode {
    int value;
    SingleLinkNode next=null;

    public SingleLinkNode(int value) {
        this.value = value;
    }
}